package com.turing.post.transaction.impl;

import java.util.Date;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;

import com.turing.post.organization.util.IssueCompany;
import com.turing.post.organization.util.IssueCompanyDao;
import com.turing.post.organization.util.Merchant;
import com.turing.post.organization.util.MerchantDao;
import com.turing.post.transaction.util.MchAccDetailDao;
import com.turing.post.transaction.util.MchChkAccCtl;
import com.turing.post.transaction.util.MchChkAccCtlBusiness;
import com.turing.post.transaction.util.MchChkAccCtlDao;
import com.turing.post.transaction.util.MchChkAccCtlSummary;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;

@Stateless(mappedName = "transaction.impl.MchChkAccCtlBusinessImpl")
@Remote
@Local
public class MchChkAccCtlBusinessImpl implements MchChkAccCtlBusiness {

	@EJB(mappedName = "organization.impl.IssueCompanyDaoImpl")
	private IssueCompanyDao issueCompanyDaoImpl;

	@EJB(mappedName = "organization.impl.MerchantDaoImpl")
	private MerchantDao merchantDaoImpl;

	// @EJB(mappedName = "transaction.impl.MchTradDetailDaoImpl")
	// private MchTradDetailDao mchTradDetailDaoImpl;

	@EJB(mappedName = "transaction.impl.MchAccDetailDaoImpl")
	private MchAccDetailDao MchAccDetailDaoImpl;

	@EJB(mappedName = "transaction.impl.MchChkAccCtlDaoImpl")
	private MchChkAccCtlDao MchChkAccCtlDaoImpl;

	/**
	 * 根据发卡商NO以及日期统计对帐汇总情况
	 * 
	 * @param issueCompanyNo
	 * @param tranDate
	 * @return
	 */
	public MchChkAccCtlSummary getMchChkAccCtlSummaryByTranDate(
			String issueCompanyNo, Date tranDate)
			throws PCPSApplicationException {

		if (PCPSUtil.isEmpty(issueCompanyNo))
			throw new PCPSApplicationException("", "发卡商参数非法!");

		// 取发卡商
		IssueCompany issueCompany = issueCompanyDaoImpl
				.getIssueCompanyByIssueCompanyNo(issueCompanyNo);
		if (issueCompany == null)
			throw new PCPSApplicationException("", String.format("无此发卡商%s!",
					issueCompanyNo));

		if (tranDate == null
				|| issueCompany.getTranDate().compareTo(tranDate) < 0)
			throw new PCPSApplicationException("", "帐务日期非法!");

		MchChkAccCtlSummary mcacs = new MchChkAccCtlSummary();

		List<Merchant> mchlist = merchantDaoImpl
				.getMerchantListByIssueCompanyNo(issueCompanyNo);

		// 总的商户数
		mcacs.all = mchlist.size();

		for (Merchant mch : mchlist) {
			boolean hastran = MchAccDetailDaoImpl.merchantHasTransaction(mch
					.getMerchantNo(), tranDate, tranDate);
			// 有交易的商户
			if (hastran)
				mcacs.hastrans++;

			// 无需联机对帐的商户
			if (PCPSUtil.compareObject(mch.getCheckAccMode(),
					Merchant.CHECKACCMODE_NOACC)) {
				mcacs.noneedacc++;
				continue;
			}

			// 无交易继续
			if (!hastran)
				continue;

			// 有交易发生且需要联机对帐的商户
			mcacs.hastrans_needacc++;

			// 检查对帐控制文件
			MchChkAccCtl mcac = MchChkAccCtlDaoImpl.getMchChkAccCtlByKey(mch
					.getMerchantNo(), tranDate);
			// 对帐平的商户
			if (mcac != null
					&& PCPSUtil.compareObject(mcac.getCheckResult(),
							MchChkAccCtl.CHECKRESULT_SUCC))
				mcacs.hastrans_needacc_success++;
			else {
				mcacs.hastrans_needacc_noacc++;
				mcacs.noacc_list.add(mch);
			}
		}

		return mcacs;
	}
}